//解决android定位fixed或者absolute随键盘弹起
export const adPosition = {
  data() {
    return {
      screenHeight: 0,
      initBodyHeight: 0
    }
  },
  mounted() {
    const _this = this;
    this.initBodyHeight = document.body.clientHeight;
    window.onresize = () => {
      _this.screenHeight = document.body.clientHeight;
    }
  },
  watch: {
    screenHeight(newVal) {
      this.screenHeight = newVal;
      let $parent = document.getElementsByClassName('router')[0];
      if (this.screenHeight < this.initBodyHeight) {
        $parent.style.transition = 'none';
        this.$refs.save.style.display = 'none';
      } else if (this.screenHeight === this.initBodyHeight) {
        this.$refs.save.style.display = 'block';
        $parent.style.transition = '';
      }
    }
  }
};

//登录注册单独处理。隐藏原生状态栏，重新设置当前容器高度
export const hideStatus = {
  beforeRouteEnter(to, from, next) {
    if ($prod) {
      let json = JSON.parse(window.localStorage.getItem('STORE'));
      if (json && json.token) {
        StatusBar.show();
      } else {
        StatusBar.hide();
      }
    }
    next()
  },
  mounted() {
    this.$refs.fullScreen.style.height = document.body.clientHeight + 'px';
  }
}

import TransferDom from '../core/directives/transfer-dom'
import {bd09togcj02} from "../utils/commonUtils"
import {ORDER_CHANGE_DATE} from '../assets/js/api'
// import Loading from  '../components/tsui/loading'

//首页订单和订单逻辑。
export const orderMixins = {
  data() {
    return {
      //展示“备注”
      showLong: {},

      //照片查看
      imgList: [],

      //改期modal
      mdDateVisible: false,
      //改期picker
      mdTimeVisible: false,
      //订单改期表单
      mdDateForm: {
        //修改描述
        rescheduled_events: '',
        //改期时间
        edit_time: null,
        //订单id
        order_id: null,
        //临时显示日期格式
        temp_time: '',
        //临时索引
        num: null
      },

      firstLoad: true,
      topConfig: {
        triggerDistance: 50 // 下拉刷新触发的距离
      },
      iconLink: ''
    }
  },
  directives: {
    TransferDom
  },
  components: {
    previewer(resolve) {
      require(['../components/tsui/previewer/previewer.vue'], resolve);
    },
    MdTimePicker(resolve) {
      require(['../components/module/MdTimePicker.vue'], resolve);
    }
  },
  methods: {
    showLongValues(index) {
      console.log(this.showLong)
      if (this.showLong.hasOwnProperty(index)) {
        this.$delete(this.showLong, index);
      } else this.$set(this.showLong, index, true);
    },
    //设置当前选项卡
    async setCurrTabIndex(tab) {
      this.currentTabIndex = tab;
      this.filterItem[tab].loading = true;
      let jsonData = await this.getOrders({tab});
      this.$set(this.orderList, tab, jsonData);
      this.reComputeOrder();
      this.$nextTick(() => {
        this.filterItem[tab].loading = false;
      });
      this.filterItem[tab].page = 1;
    },
    //刷新
    async refresh(loaded) {
      let index = this.currentTabIndex;
      this.orderList[index] = await this.getOrders({tab: index, pull: true});
      this.filterItem[index].allLoaded = this.orderList[index].length < 10;
      loaded('done');
    },
    //加载更多
    async loadMore() {
      //初始化完成后
      if (!this.filterItem[this.currentTabIndex].allLoaded) {
        let index = this.currentTabIndex;
        let page = this.filterItem[index].page += 1;
        let jsonData = await this.getOrders({next: false, page, pull: true});
        if (jsonData.length === 0) {
          this.filterItem[index].allLoaded = true;
        } else {
          this.$set(this.orderList, index, this.orderList[index].concat(jsonData));
        }
      }
    },
    //重新计算订单数量
    reComputeOrder() {
      if (this.orderList[this.currentTabIndex].length < 3) {
        this.filterItem[this.currentTabIndex].allLoaded = true;
      }
    },
    //下拉状态改变
    stateChange(state) {
      if (state === 'pull' || state === 'trigger') {
        this.iconLink = '#icon-arrow-bottom';
      } else if (state === 'loading') {
        this.iconLink = '#icon-loading';
      } else if (state === 'loaded-done') {
        this.iconLink = '#icon-finish';
      }
    },
    //照片查看
    showPhoto(image) {
      this.imgList = [];
      image.map((item, key, arr) => {
        this.imgList.push({src: item});
      });
      this.$nextTick(() => {
        this.$refs.previewer.show(0)
      });
    },

    //lat 纬度 lon 经度
    goAmap({lon, lat, destName}) {
      if ($prod) {
        // 进行定位
        baidumap_location.getCurrentPosition((result) => {
          const {latitude, longitude} = result;
          console.log(result)
          const start = bd09togcj02(longitude, latitude).join(',');
          // const dest = bd09togcj02(lon, lat).join(',');
          const dest = `${lon},${lat}`;
          console.log(dest)
          this.$router.push({path: 'order/orderMap', query: {start, dest, destName}});
        }, (error) => {
          alert(error);
        });
      } else {
        const start = '116.444118,39.927925';
        const dest = bd09togcj02(lon, lat).join(',');
        console.log(lon)
        this.$router.push({path: 'order/orderMap', query: {start, dest, destName}});
      }
    },
    //显示改期模态框
    showMdModal(order_id, num) {
      this.mdDateVisible = true;
      this.mdDateForm.order_id = order_id;
      this.mdDateForm.num = num;
    },
    //显示改期时间picker
    showTimeSetting() {
      this.mdTimeVisible = true;
      setTimeout(() => {
        document.getElementsByClassName('v-modal')[0].style.background = 'transparent';
      }, 0);
    },
    //改期时间picker完成选择回掉
    finishMdTime(time) {
      this.mdDateForm.temp_time = time;
      this.mdDateForm.edit_time = new Date(time).getTime() / 1000;
    },
    //改期模态框确认回掉
    async mdOrderDate() {
      if (!this.mdDateForm.rescheduled_events) {
        this.$toast({message: '请填写理由', position: 'top'});
        return;
      }
      if (!this.mdDateForm.edit_time) {
        this.$toast({message: '请选择日期', position: 'top'});
        return;
      }
      let json = await this.$http.post(ORDER_CHANGE_DATE, this.mdDateForm, {show: true, type: 'mini'});
      if (json.result === 1) {
        this.orderList[this.currentTabIndex][this.mdDateForm.num].make_time = this.mdDateForm.edit_time;
        this.$toast({message: '改期成功', position: 'bottom'});
        this.mdDateForm.rescheduled_events = '';
        this.mdDateForm.temp_time = '';
        if (this.currentTabIndex === 3) {
          let currentOrderItem = this.orderList[this.currentTabIndex];
          currentOrderItem.splice(this.mdDateForm.num, 1);
          this.reComputeOrder();
        }
        if(this.currentTabIndex===0){
          this.orderList[this.currentTabIndex][this.mdDateForm.num].order_status=2
        }
        this.mdDateForm.rescheduled_events = '';
        this.mdDateForm.temp_time = '';
        this.mdDateVisible = false;
      }
    },
  }
};
